//package com.base.service.impl;
//
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONArray;
//import com.alibaba.fastjson.JSONObject;
//import com.base.model.HarvesterCompanyVM;
//import com.base.service.PdfFileService;
//import com.base.test.PDFToImageUtil;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.stereotype.Service;
//import org.springframework.util.ResourceUtils;
//import org.springframework.util.StringUtils;
//import org.springframework.web.multipart.MultipartFile;
//
//import java.io.File;
//import java.util.UUID;
//
//@Slf4j
//@Service
//public class PdfFileServiceImpl implements PdfFileService {
//
//    @Override
//    public HarvesterCompanyVM getHarvesterCompany(MultipartFile multipartFile) {
//        String fileName = multipartFile.getOriginalFilename();
//        String suffix = fileName.substring(fileName.lastIndexOf(".") + 1);
//        try {
//            // uuid 生成文件名
//            String uuid = String.valueOf(UUID.randomUUID());
//            // 根路径，在 resources/static/upload/uuid
//            String filePath = ResourceUtils.getURL("classpath:").getPath() + "static/upload/" + uuid + "/";
//            // 创建新的文件
//            File fileExist = new File(filePath);
//            // 文件夹不存在，则新建
//            if (!fileExist.exists()) {
//                fileExist.mkdirs();
//            }
//            // 把文件写入到本地
//            multipartFile.transferTo(new File(filePath, fileName));
//            // 如果是pdf
//            if (suffix.equalsIgnoreCase("pdf")) {
//                // 转成图片
//                fileName = PDFToImageUtil.PDFToImage(filePath, fileName);
//            }
//            // 去除水印
//            PDFToImageUtil.clearWaterMark(filePath, fileName);
//            // 图片转成base64字符串
//            String base64Str = PDFToImageUtil.convertImageToBase64Str(filePath, fileName);
//            System.out.println(base64Str);
//            String reqBody = "object_type=general_v5&disp_line_poly=true&type=st_ocrapi&languagetype=CHN_ENG&eng_granularity=word&imgDirection=setImgDirFlag&image=" + base64Str;
//            String reqBodyBase64 = PDFToImageUtil.encode(reqBody);
//            String resBody = "{\"err_no\":0,\"err_msg\":\"SERVER_SUCCESS\",\"result\":\"eyJsb2dpZCI6IiIsImVycl9ubyI6LTE0MDAsImVycl9tc2ciOiJJTlBVVF9KU09OX0VSUk9SX1BBUlNFX0VSUk9SIiwicXVlcnlzaWduIjoiIiwiaW1hZ2VfaW5mbyI6eyJpbWFnZV9kaXIiOjAsImxhbmdfdHlwZV9tYXAiOltdfSwicmV0IjpbXSwicGFyYWdyYXBocyI6W119\",\"calc_time_ms\":3,\"wait_time_ms\":3}";
//            JSONObject jsonObject = JSONObject.parseObject(resBody);
//            String result = jsonObject.getString("result");
//            String decode = PDFToImageUtil.decode(result);
//            JSONObject decodeJson = JSONObject.parseObject(decode);
//            JSONArray ret = decodeJson.getJSONArray("ret");
//            HarvesterCompanyVM harvesterCompanyVM = new HarvesterCompanyVM();
//            for (int i = 0; i < ret.size(); i++) {
//                JSONObject json = ret.getJSONObject(i);
//                String word = json.getString("word");
//                if (!StringUtils.isEmpty(word)) {
//                    if (word.contains("收货地点")) {
//                        String address = word.replace("收货地点:", "").trim();
//                        harvesterCompanyVM.setCompanyAddress(address);
//                    } else if (word.contains("收货单位")) {
//                        String name = word.replace("收货单位:", "").trim();
//                        harvesterCompanyVM.setCompanyName(name);
//                    }
//                }
//            }
//            // System.out.println(decode);
//            return null;
//        } catch (Exception e) {
//            log.error("出现未知异常",e);
//        }
//        return null;
//    }
//}
